package itsm.isperp.module.controller.configuration;

import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.configuration.ConfCi;
import itsm.isperp.module.entity.configuration.ConfCiRelation;
import itsm.isperp.module.entity.configuration.ConfCiRelationType;
import itsm.isperp.module.service.configuration.ConfCiRelationService;
import itsm.isperp.module.service.configuration.ConfCiRelationTypeService;
import itsm.isperp.module.service.configuration.ConfCiService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 配置关系控制器
 * 
 * @author lizx
 * @date 2014-09-22 09:52
 */
@Controller
@RequestMapping("configuration/ciRelation")
public class ConfCiRelationController extends BaseController<ConfCiRelation> {

	@Autowired
	protected ConfCiRelationService confCiRelationService;
	@Autowired
	protected ConfCiRelationTypeService confCiRelationTypeService;
	@Autowired
	protected ConfCiService confCiService;

	@Override
	public ConfCiRelationService getService() {
		return this.confCiRelationService;
	}

	@Override
	public String getPageTitle() {
		return "配置关系";
	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {
		String fromId = req.getParameter("fromId");
		request.setSearchString("from_id='" + fromId + "' OR to_id='" + fromId
				+ "'");
		return this.getService().findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultMessage delete(String id, HttpServletRequest req) {
		String changeId = req.getParameter("changeId");
		this.getService().delete(changeId, id);

		return ResultMessage.newInstance(true);
	}

	/**
	 * 批量保持关联
	 * 
	 * @param changeId
	 * @param relationTypeId
	 * @param fromId
	 * @param toIds
	 * @param req
	 * @return
	 */
	@RequestMapping("batchSave")
	@ResponseBody
	public ResultMessage batchSave(String changeId, String relationTypeId,
			String fromId, String toIds, HttpServletRequest req) {

		String[] toIdArr = toIds.split(",");

		this.confCiRelationService.batchSave(changeId, relationTypeId, fromId,
				toIdArr);
		return ResultMessage.success();
	}

	@RequestMapping("saveCiRelation")
	@ResponseBody
	public ResultMessage saveCiRelation(String fromId, String relationType,
			String toCiIds, HttpServletRequest req) {
		String[] toIds = toCiIds.split(",");

		if (toIds.length == 1) {
			ConfCiRelation entity = new ConfCiRelation();
			entity.setFromId(fromId);
			entity.setRelationTypeId(relationType);
			entity.setToId(toIds[0]);
			this.getService().saveOrUpdate(entity);

			return ResultMessage.success(entity.getId());
		} else {
			for (String s : toIds) {
				ConfCiRelation entity = new ConfCiRelation();
				entity.setFromId(fromId);
				entity.setRelationTypeId(relationType);
				entity.setToId(s);
				this.getService().saveOrUpdate(entity);
			}

		}
		return ResultMessage.success();
	}

	@RequestMapping("saveCiRelationIsToCi")
	@ResponseBody
	public ResultMessage saveCiRelationIsToCi(String fromId,
			String relationType, String toCiIds, HttpServletRequest req) {
		String[] fromIds = fromId.split(",");

		if (fromIds.length == 1) {
			ConfCiRelation entity = new ConfCiRelation();
			entity.setToId(toCiIds);
			entity.setRelationTypeId(relationType);
			entity.setFromId(fromIds[0]);
			this.getService().saveOrUpdate(entity);

			return ResultMessage.success(entity.getId());
		} else {
			for (String s : fromIds) {
				ConfCiRelation entity = new ConfCiRelation();
				entity.setToId(toCiIds);
				entity.setRelationTypeId(relationType);
				entity.setFromId(s);
				this.getService().saveOrUpdate(entity);
			}

		}

		return ResultMessage.success();
	}

	@RequestMapping("listCiRelation")
	@ResponseBody
	public List<Map<String, Object>> listCiRelation(String ciId,
			HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		return this.getService().listCiRelation(ciId);
	}

	@RequestMapping("listRelateCi")
	@ResponseBody
	public DataResponse<Map<String, Object>> listRelateCi(String ciId,
			String categoryId, String type, HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		JqGridRequest request = new JqGridRequest();
		String filter = "";
		filter = " (treenode_path like '%" + categoryId
				+ "%' and relation_ci_id='" + ciId + "')";
		request.setSearchString(filter);
		if (type.equals("from_ci")) {
			return this.getService().findRelateToCiByRequest(request);
		} else if (type.equals("to_ci")) {
			return this.getService().findRelateFromCiByRequest(request);
		} else {
			return null;
		}
	}

	@RequestMapping("listCiRelationType")
	@ResponseBody
	public List<Map<String, Object>> listCiRelationType(String categoryId,
			HttpServletRequest req) {
		// 根据ciId 获取有资产数据的关系
		return this.getService().listCiRelationType(categoryId);
	}

	/**
	 * 列出关联关系的ci
	 * 
	 * @param fromId
	 * @param relationTypeId
	 * @param request
	 * @param req
	 * @return
	 */
	@RequestMapping("listRelation")
	@ResponseBody
	public DataResponse<ConfCi> listRelation(String fromId,
			String relationTypeId, JqGridRequest request, HttpServletRequest req) {
		// 根据fromId获取它的分类
		// 获取可以的关联列表
		ConfCiRelationType rt = this.confCiRelationTypeService
				.findOne(relationTypeId);

		request.addFilterRule("conf_category_id", JqGridFilterRuleOp.eq,
				rt.getToCategoryId());

		request.addFilterRule("id", JqGridFilterRuleOp.ne, fromId);
		request.setSearchString("  id NOT IN (SELECT to_id FROM conf_ci_relation WHERE from_id='"
				+ fromId + "')");

		return this.confCiService.findAll(request);
	}

	/**
	 * 重建所有关联关系
	 * 
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("rebuildAll")
	@ResponseBody
	public ResultMessage rebuildAll(HttpServletRequest req) throws Exception {

		String id = req.getParameter("id");
		String relationTypeId = req.getParameter("relationTypeId");

		if (StringUtils.isNotEmpty(id)) {
			String changeId = req.getParameter("changeId");
			this.confCiRelationService.rebuildCiRelationByCi(id, changeId);
		} else if (StringUtils.isNotEmpty(relationTypeId)) {
			this.confCiRelationService
					.rebuildCiRelationByRelationType(relationTypeId);
		} else {
			this.confCiRelationService.rebuildCiRelation();
		}

		return ResultMessage.success();
	}

	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String categoryId = req.getParameter("categoryId");
		if (StringUtils.isNotEmpty(categoryId)) {
			request.setSearchString(" b.treenode_path like '%" + categoryId
					+ "%'");
		}
		return this.confCiService.findAllDetailByRequest(request).toPage();
	}

	@RequestMapping("listCiEx")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCiEx(JqGridRequest request,
			HttpServletRequest req) {
		String categoryId = req.getParameter("categoryId");
		String relationTypeId = req.getParameter("relationTypeId");
		String ciId = req.getParameter("ciId");
		String ty = req.getParameter("ty");
		if (StringUtils.isNotEmpty(categoryId)) {
			if (ty.equals("from_ci")) {
				request.setSearchString(" (b.treenode_path like '%"
						+ categoryId
						+ "%') and  a.id not in (select to_id from conf_ci_relation where relation_type_id='"
						+ relationTypeId + "' and from_id='" + ciId + "')");

			} else {
				request.setSearchString(" (b.treenode_path like '%"
						+ categoryId
						+ "%') and  a.id not in (select from_id from conf_ci_relation where relation_type_id='"
						+ relationTypeId + "' and to_id='" + ciId + "')");
			}
		}
		return this.confCiService.findAllDetailByRequest(request).toPage();
	}

	@RequestMapping("getCiRelateCount")
	@ResponseBody
	public Integer getCiRelateCount(String ciId, HttpServletRequest req) {
		return this.getService().getCiRelateCount(ciId);
	}

}
